#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<qrect.h>
#include<qtimer.h>
#include<QTimer>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
#include<QtGlobal>
#include<QTime>
#include<ctime>
#include<QPixmap>
#include<QPushButton>
#include<tools.h>
#include"assignment.h"
#include"equipmentshop.h"
#include"balance_account.h"
#include"message.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , numberfinished(0),ui(new Ui::MainWindow),speed(50),blsRun(false),havespeed(0),canspeed(1),measurespeed(0),blsOver(false),timenokill(0),timekill(0)
    , islight(1),staytime(0),istouch(false),iskilling(false),lightspread(100),ghostspeed(3.5),numbershengmu(0),havecolded(false),shengmu_killing(false),times1(0)
{
    ui->setupUi(this);
    this->setGeometry(QRect(435,100,700,700));//第三格是x轴
    QPushButton *btn2=new QPushButton("任务栏",this);
    btn2->move(600,15);
    connect(btn2,&QPushButton::clicked,this,&MainWindow::gotoassignment);
    QPushButton *btn3=new QPushButton("发车",this);
    btn3->move(600,55);
    connect(btn3,&QPushButton::clicked,this,&MainWindow::traindepature);
    assignmentstore.resize(5);
    assignmentstore[0]="逃杀";
    assignmentstore[1]="刺骨寒温";
    assignmentstore[2]="在鬼附近使用圣木";
    assignmentstore[3]="在鬼猎杀时使用圣木驱散鬼魂";
    assignmentstore[4]="踩盐";
    setFixedSize(700,700);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent*event)//update()或程序第一次启动时自动执行，用于画图
{
    if(islight%2==0)
    {
        lightspread=150;
    }
    else
    {
        lightspread=100;
    }
    nDirection=0;//不输入就不动
    QPainter painter(this);
    //初始化
    if(blsRun==false)
    {
        InitMainWindow();
    }
    //if(times%2==1||times==0||times==2||times==4)//明暗变换
    //{
    //画游戏背景
    //外墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15,15,570,570);//画矩形，相对于窗体的起点坐标（15，15），大小260*260

    //内墙
   // painter.setPen(Qt::black);
    //painter.setBrush(Qt::black);
    //painter.drawRect(20,20,560,560);
    painter.drawPixmap(20,20,560,560,QPixmap(":/images/myImages/u=1904022650,3051689427&fm=253&fmt=auto&app=138&f=JPEG.png"));

    //画小格子，大小10*10
    //painter.setPen(Qt::blue);//设置画笔颜色
    //for(int i=2;i<=58;i++)
    //{
      //  painter.drawLine(20,i*10,580,i*10);//竖线
        //painter.drawLine(i*10,20,i*10,580);//横线
    //}

    //画场内地图
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(20,295,120,10);//横线1/4
    painter.drawRect(157,295,150,10);
    painter.drawRect(305,295,127,10);
    painter.drawRect(453,295,127,10);
    painter.drawRect(295,20,10,120);//竖线1/2
    painter.drawRect(295,157,10,145);
    painter.drawRect(295,305,10,127);
    painter.drawRect(295,453,10,127);
    painter.drawRects(&vDesks[0],vDesks.size());//桌子
    //画冒险家
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawRect(adventurer);
    //画鬼
    //if(iskilling)
    //{
     painter.setPen(Qt::black);
     painter.setBrush(Qt::red);
     painter.drawRect(ghost);
     painter.drawPixmap(ghost.left(),ghost.top(),10,10,QPixmap(":/images/myImages/u=1147999306,579059087&fm=253&fmt=auto&app=138&f=JPEG.png"));
    //}
    //使视野只能是周身lighrspread格的方块
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    if(adventurer.top()-lightspread>20)
    {
      painter.drawRect(20,20,560,adventurer.top()-lightspread);
    }
    if(adventurer.left()-lightspread>20)
    {
    painter.drawRect(20,20,adventurer.left()-lightspread,560);
    }
    if(adventurer.bottom()+lightspread<580)
    {
    painter.drawRect(20,adventurer.bottom()+lightspread,560,580-lightspread-adventurer.bottom());
    }
    if(adventurer.right()+lightspread<580)
    {
    painter.drawRect(adventurer.right()+lightspread,20,580-lightspread-adventurer.right(),560);
    }
  // }
    /* else
    {
        painter.setPen(Qt::black);
        painter.setBrush(Qt::black);
        painter.drawRect(15,15,570,570);
    }*/
    //显示游戏开始，结束
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(220,270,sDisplay);
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(570,640,QString::number(array[adventurer.left()-20][adventurer.top()-20]));
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(500,640,QString("温度:"));
    painter.drawText(20,640,"圣木数量");
    painter.drawText(100,640,QString::number(numbershengmu));//画圣木
    if(array[adventurer.left()-20][adventurer.top()-20]<4)
        havecolded=true;
    if(blsOver)
    {
        timer->stop();
        timer1->stop();
        timer2=new QTimer(this);
        connect(timer2,SIGNAL(timeout()),this,SLOT(MainWindow_update2()));
        timer2->start(1000);
    }
    if(times1==5)
    {
        timer2->stop();
       traindepature();
    }
}
void MainWindow::InitMainWindow()
{
    numbershengmu=this->equipmentshop1->takeawayshengmu;
    shengmusended=numbershengmu;
    times=0;
    blsRun=true;
    QRect rect(100,70,10,10);//初始冒险家
    adventurer=rect;
    adventurerSXZY.resize(4);//初始冒险家四个角的坐标
    srand(QTime(0,0,0).secsTo(QTime::currentTime()));//初始鬼坐标
    ghostix=20+rand()%(580-20);
    ghostiy=20+rand()%(580-20);
    QRect rect0(ghostix,ghostiy,10,10);
    ghost=rect0;
    killStartime=40+rand()%(60-40);
     vDesks.resize(4);//初始化四个障碍物
    QRect rect1(120,140,80,40),rect2(400,140,80,40),rect3(140,400,40,80),rect4(430,420,40,80);//分别是左上 右上 左下 右下
     vDesks[0]=rect1;
     vDesks[1]=rect2;
     vDesks[2]=rect3;
     vDesks[3]=rect4;
    nDirection=0;//默认向下运动
    sDisplay="欢迎来到鬼房";
    tools.initempureter(array,ghost);//初始化温度
    q.resize(3);//表示选的是任务库里面的第几个
    nowassignment.resize(3);
     for(int i=0;i<3;i++)
    {
        int n=0+rand()%(4-0);
        while(number(i,q,n))
        {
            n=0+rand()%(4-0);
        }
        nowassignment[i]=assignmentstore[n];
        q[i]=n;
    }
    timer=new QTimer(this);//设定计时器
    connect(timer,SIGNAL(timeout()),this,SLOT(MainWindow_update()));
    timer->start(speed);
    timer1=new QTimer(this);
    connect(timer1,SIGNAL(timeout()),this,SLOT(MainWindow_update1()));
    timer1->start(1000);

}
void MainWindow::MainWindow_update()
{
    if(nDirection==9&&numbershengmu>0)
    {
    numbershengmu--;
    tools.pausanto(adventurer,ghost,istouch);
    }
    if(iskilling==false)
    {
        timenokill++;
        if(timenokill==(killStartime)*20+1)
        {
            timenokill=0;
            iskilling=true;
            killOvertime=6+rand()%(15-6);
        }
    }
    else
    {
        havekilled=true;
        timekill++;
        if(timekill==(killOvertime)*20+1)
        {
            iskilling=false;
            killStartime=20+rand()%(60-20);
            timekill=0;
            QRect rectn(ghostix,ghostiy,10,10);
            ghost=rectn;
        }
        else
        {
            if(istouch)
            {
                shengmu_killing=true;
                staytime++;
                if(staytime*0.05==2)
                {
                    istouch=false;
                    staytime=0;
                }
            }
            else
            {
            moveghost();
            }
        }
    }
    int speednow=3;
    if((nDirection==5||nDirection==6||nDirection==7||nDirection==8)&&canspeed)
    {
        havespeed=true;
        measurespeed++;
        if(measurespeed==41)
        {
            measurespeed=0;
            canspeed=false;

        }
        else
        {
            speednow=4;
        }
    }
    if(nDirection>4)
    {
        nDirection=nDirection-4;
    }
    if(havespeed==true&&canspeed==false)
    {
        measurespeed++;
        if(measurespeed==61)
        {
            canspeed=true;
            measurespeed=0;
        }
    }
    switch(nDirection)
    {
    case 1:
        adventurer.moveTo(adventurer.left(),adventurer.top()-speednow);
        //adventurer.setTop(adventurer.top()-speednow);
        //adventurer.setBottom(adventurer.bottom()-speednow);
        break;
    case 2:
        adventurer.moveTo(adventurer.left(),adventurer.top()+speednow);
        //adventurer.setTop(adventurer.top()+speednow);
        //adventurer.setBottom(adventurer.bottom()+speednow);
        break;
    case 3:
        adventurer.moveTo(adventurer.left()-speednow,adventurer.top());
        //adventurer.setLeft(adventurer.left()-speednow);
        //adventurer.setRight(adventurer.right()-speednow);
        break;
    case 4:
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
        break;
    case 5:
        break;
    default:;
    }

    adventurerSXZY[0].setX(adventurer.left());
    adventurerSXZY[0].setY(adventurer.top());
    adventurerSXZY[1].setX(adventurer.right());
    adventurerSXZY[1].setY(adventurer.top());
    adventurerSXZY[2].setX(adventurer.left());
    adventurerSXZY[2].setY(adventurer.bottom());
    adventurerSXZY[3].setX(adventurer.right());
    adventurerSXZY[3].setY(adventurer.bottom());
    if(adventurer.left()<20)
    {
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
    }
    if(adventurer.right()>580)
    {
        adventurer.setRight(adventurer.right()-speednow);
        adventurer.setLeft(adventurer.left()-speednow);
    }
    if(adventurer.top()<20)
    {
        adventurer.setTop(adventurer.top()+speednow);
        adventurer.setBottom(adventurer.bottom()+speednow);
    }
    if(adventurer.bottom()>580)
    {
        adventurer.setBottom(adventurer.bottom()-speednow);
        adventurer.setTop(adventurer.top()-speednow);
    }
    if((adventurer.left()>140&&adventurer.right()<157)||(adventurer.left()>432&&adventurer.right()<453))
    {

    }
    else
    {
      if(isinwall(adventurer.top(),adventurer.bottom(),290,310)==1)
      {
        adventurer.setBottom(adventurer.bottom()+speednow);
        adventurer.setTop(adventurer.top()+speednow);
      }
      else if(isinwall(adventurer.top(),adventurer.bottom(),290,310)==2)
      {
        adventurer.setBottom(adventurer.bottom()-speednow);
        adventurer.setTop(adventurer.top()-speednow);
      }
    }
    if((adventurer.top()>140&&adventurer.bottom()<157)||(adventurer.top()>432&&adventurer.bottom()<453))
    {

    }
    else
    {
      if(isinwall(adventurer.left(),adventurer.right(),290,310)==1)
      {
        adventurer.setLeft(adventurer.left()+speednow);
        adventurer.setRight(adventurer.right()+speednow);
      }
      if(isinwall(adventurer.left(),adventurer.right(),290,310)==2)
      {
        adventurer.setLeft(adventurer.left()-speednow);
        adventurer.setRight(adventurer.right()-speednow);
      }
    }
    for(int i=0;i<4;i++)
    {
        int n=0;
        n=isinRect(adventurerSXZY[i],vDesks);
        if(n!=0)
        {
            move(nDirection,i,speednow);
            break;
        }
    }
    int m=0;
    QVector<QRect> ghost1;
    ghost1.resize(1);
    ghost1[0]=ghost;
    if(iskilling)
    {
    for(int i=0;i<4;i++)
    {
        m=isinRect(adventurerSXZY[i],ghost1);
        if(m!=0)
        {
            blsOver=true;
            sDisplay="游戏结束";

        }
    }
    }
    update();//paintEvent更新
}
void MainWindow:: MainWindow_update1()
{
    times++;
    if(times==2)
    {
        sDisplay="";
    }
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    case Qt::Key_W:nDirection=5;
        break;
    case  Qt::Key_S:nDirection=6;
        break;
    case  Qt::Key_A:nDirection=7;
        break;
    case  Qt::Key_D:nDirection=8;
        break;
    case  Qt::Key_J:islight=islight+1;
        break;
    case  Qt::Key_K:nDirection=9;
    default:;
    }
}
/*void MainWindow::modifiers(QKeyEvent *key)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_Shift:Speedup=1;
    }
}*/
int MainWindow::isinwall(int advLT,int advRB,int wallLT,int wallRB)
{
    if(advLT<wallRB&&advLT>wallLT)
    {
        return 1;
    }
    else if(advRB<wallRB&&advRB>wallLT)
    {
        return 2;
    }
    return 0;
}

/*int MainWindow::isinRect(int advL,int advR,int advT,int advB,int rectL,int rectR,int rectT,int rectB)
{
    if()
}*/
int MainWindow::isinRect(QPoint a,QVector<QRect> b)
{
    for(int i=0;i<b.size();i++)
    {
    if((a.x()>b[i].left())&&a.x()<b[i].right()&&a.y()>b[i].top()&&a.y()<b[i].bottom())
    {
            return (i+1);
    }
    }
    return 0;
}
void MainWindow::move(int direction,int whichjiao,int speednow)
{
    if(whichjiao==0)
    {
        if(direction==3)
        {
            adventurer.setLeft(adventurer.left()+speednow);
            adventurer.setRight(adventurer.right()+speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()+speednow);
            adventurer.setBottom(adventurer.bottom()+speednow);
            return ;
        }


    }
    if(whichjiao==1)
    {
        if(direction==4)
        {
            adventurer.setLeft(adventurer.left()-speednow);
            adventurer.setRight(adventurer.right()-speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()+speednow);
            adventurer.setBottom(adventurer.bottom()+speednow);
            return ;
        }
    }
    if(whichjiao==2)
    {
        if(direction==3)
        {
            adventurer.setLeft(adventurer.left()+speednow);
            adventurer.setRight(adventurer.right()+speednow);
            return;
        }
        else
        {
            adventurer.setTop(adventurer.top()-speednow);
            adventurer.setBottom(adventurer.bottom()-speednow);
            return ;
        }
    }
    else if(whichjiao==3)
        {
            if(direction==4)
            {
                adventurer.setLeft(adventurer.left()-speednow);
                adventurer.setRight(adventurer.right()-speednow);
                return;
            }
            else
            {
                adventurer.setTop(adventurer.top()-speednow);
                adventurer.setBottom(adventurer.bottom()-speednow);
                return ;
            }
        }
}
void MainWindow::moveghost()
{
    if(isinsight(adventurer,ghost)&&islight)
    {
        ghostspeed=5;
    }
    int q1=0,q2=0;
    if(abs(ghost.left()-adventurer.left())>abs(ghost.top()-adventurer.top()))
    {
        if(ghost.left()>adventurer.left())
        {
            QPoint t1(ghost.left()-ghostspeed,ghost.top());
            QPoint t2(ghost.left()-ghostspeed,ghost.bottom());
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.top()>adventurer.top())
                {
                    ghost.setBottom(ghost.bottom()-ghostspeed);
                    ghost.setTop(ghost.top()-ghostspeed);
                }
                else
                {
                    ghost.setBottom(ghost.bottom()+ghostspeed);
                    ghost.setTop(ghost.top()+ghostspeed);
                }
            }
            else
            {
            ghost.setLeft(ghost.left()-ghostspeed);
            ghost.setRight(ghost.right()-ghostspeed);
            }
        }
        else
        {
            QPoint t1(ghost.right()+ghostspeed,ghost.top());
            QPoint t2(ghost.right()+ghostspeed,ghost.bottom());
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.top()>adventurer.top())
                {
                    ghost.setBottom(ghost.bottom()-ghostspeed);
                    ghost.setTop(ghost.top()-ghostspeed);
                }
                else
                {
                    ghost.setBottom(ghost.bottom()+ghostspeed);
                    ghost.setTop(ghost.top()+ghostspeed);
                }
            }
            else
            {
            ghost.setLeft(ghost.left()+ghostspeed);
            ghost.setRight(ghost.right()+ghostspeed);
            }
        }
    }
    else
    {
        if(ghost.top()>adventurer.top())
        {
            QPoint t1(ghost.left(),ghost.top()-ghostspeed);
            QPoint t2(ghost.right(),ghost.top()-ghostspeed);
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.left()>adventurer.left())
                {
                    ghost.setLeft(ghost.left()-ghostspeed);
                    ghost.setRight(ghost.right()-ghostspeed);
                }
                else
                {
                    ghost.setLeft(ghost.left()+ghostspeed);
                    ghost.setRight(ghost.right()+ghostspeed);
                }
            }
            else
            {
                ghost.setTop(ghost.top()-ghostspeed);
                ghost.setBottom(ghost.bottom()-ghostspeed);
            }
        }
        else
        {
            QPoint t1(ghost.left(),ghost.bottom()+ghostspeed);
            QPoint t2(ghost.right(),ghost.bottom()+ghostspeed);
            q1=isinRect(t1,vDesks);
            q2=isinRect(t2,vDesks);
            if(q1!=0||q2!=0)
            {
                if(ghost.left()>adventurer.left())
                {
                    ghost.setLeft(ghost.left()-ghostspeed);
                    ghost.setRight(ghost.right()-ghostspeed);
                }
                else
                {
                    ghost.setLeft(ghost.left()+ghostspeed);
                    ghost.setRight(ghost.right()+ghostspeed);
                }
            }
            else
            {
               ghost.setTop(ghost.top()+ghostspeed);
              ghost.setBottom(ghost.bottom()+ghostspeed);
            }
        }
    }
    if((ghost.left()>140&&ghost.right()<157)||(ghost.left()>432&&ghost.right()<453))
    {

    }
    else
    {
        if(isinwall(ghost.top(),ghost.bottom(),290,310)==1)
        {
            ghost.setBottom(ghost.bottom()+1);
            ghost.setTop(ghost.top()+1);
        }
        else if(isinwall(ghost.top(),ghost.bottom(),290,310)==2)
        {
            ghost.setBottom(ghost.bottom()-1);
            ghost.setTop(ghost.top()-1);
        }
    }
    if((ghost.top()>140&&ghost.bottom()<157)||(ghost.top()>432&&ghost.bottom()<453))
    {

    }
    else
    {
        if(isinwall(ghost.left(),ghost.right(),290,310)==1)
        {
            ghost.setLeft(ghost.left()+1);
            ghost.setRight(ghost.right()+1);
        }
        if(isinwall(ghost.left(),ghost.right(),290,310)==2)
        {
            ghost.setLeft(ghost.left()-1);
            ghost.setRight(ghost.right()-1);
        }
    }
}
bool MainWindow::isinsight(QRect rect1,QRect rect2)
{
    if(rect1.left()-lightspread<=rect2.left()&&rect1.top()-lightspread<rect2.top()&&rect1.right()+lightspread>rect2.right()&&rect1.bottom()+lightspread>rect2.bottom())
    {
        return true;
    }
}
bool MainWindow::number(int inow,QVector<int> b,int n)
{
    for(int i=0;i<inow;i++)
    {
        if(n==b[i])
        {
            return true;
        }
    }
    return false;
}
void MainWindow::gotoassignment()
{
    this->hide();
    timer->stop();
    timer1->stop();
    Assignment *assignemnt1=new Assignment();
    assignemnt1->mainwindow1=this;
    assignemnt1->show();
}

void MainWindow::traindepature()
{
    this->close();
    for(int i=0;i<3;i++)
    {
        if(ifassignmentfinished(q[i]))
            numberfinished++;
    }
    balance_account *ui_balance_account=new balance_account(equipmentshop1->money,blsOver,shengmusended,equipmentshop1->shengmustore);
    ui_balance_account->mainwindow1=this;
    ui_balance_account->show();
}

bool MainWindow::ifassignmentfinished(int n)
{
    switch(n)
    {
    case 0:
        if(blsOver==false&&havekilled)
           return true;
        else return false;
        break;
    case 1:
        if(havecolded)
            return true;
        else return false;
        break;
    case 2:
        if(istouch)
            return true;
        else return false;
        break;
    case 3:
        if(istouch&&shengmu_killing)
            return true;
        else return false;
        break;
    case 4:
        if(havesalt)
            return true;
        else return false;
        break;
    default:return false;
   }
}
void MainWindow::MainWindow_update2()
{
    times1++;
    update();
}



